package com.peng.leetcode.tree;

/**
 * GetMinimumDifference
 * <p>
 * Created on 2022/12/6
 *
 * @author lupeng
 */
public class GetMinimumDifference1 {
    public int getMinimumDifference(TreeNode root) {
        if (root == null) {
            return 0;
        }
        dfs(root);
        return ans;
    }

    int ans = Integer.MAX_VALUE;

    void dfs(TreeNode n) {
        if (n == null) {
            return;
        }
        dfs0(n.left, n.val);
        dfs0(n.right, n.val);

        dfs(n.left);
        dfs(n.right);
    }

    void dfs0(TreeNode n, int target) {
        if (n == null) {
            return;
        }
        ans = Math.min(ans, Math.abs(n.val - target));
        dfs0(n.left, target);
        dfs0(n.right, target);
    }
}
